/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-03
 * Time: 23:01
 */
public class Solution {
    public int kthToLast(ListNode head, int k) {
        //利用快慢指针
        if (head == null) {
            return -1;
        }
        int count = 0;
        ListNode slow = head;
        ListNode fast = head;
        while (count != k - 1) {
            fast = fast.next;
            if (fast == null) {
                return -1;
            }
            count++;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }
}
